package com.gjm.tool.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

public class FileTestTwo {
    public static void main(String[] args) throws IOException {
        // 通过工具类创建writer
//        ExcelWriter writer = ExcelUtil.getWriter("C:\\Users\\gjm\\Desktop\\write.xlsx");
//        List<?> row1 = CollUtil.newArrayList("aa", "bb", "cc");
//        List<?> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1");
//        List<?> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2");
//        List<List<?>> rows = CollUtil.newArrayList(row1, row2, row3);
//        writer.write(rows, true);
//        writer.close();
//        // 关闭writer，释放内存

        mainData();
    }


    public static void mainData() {
        String filePath = "C:\\Users\\gjm\\Desktop\\客户与客商信息不一致数据清单_20240922.xlsx";
        ExcelReader reader = ExcelUtil.getReader(filePath,0);
        // 读取指定sheet页面，0表示第一个sheet
        List<List<Object>> sheetList = reader.read(0);
        // 关闭读取器，释放资源
        reader.close();
        List<List<Object>> list = sheetList.subList(2, sheetList.size());
        List<MainVO> mainList = new ArrayList<>();
        for (List<Object> objects : list) {
            mainList.add(new MainVO(String.valueOf(objects.get(1)),String.valueOf(objects.get(2)),String.valueOf(objects.get(3))));
        }
        Map<String, List<MainVO>> map = mainList.stream().collect(Collectors.groupingBy(MainVO::getCustCode));
        for (Map.Entry<String, List<MainVO>> entry : map.entrySet()) {
            if (CollectionUtil.isEmpty(entry.getValue())||entry.getValue().size() <=1){
                continue;
            }
            Set<String> nameSet = entry.getValue().stream().map(i -> i.getCustName()).collect(Collectors.toSet());
            Set<String> certNoSet = entry.getValue().stream().map(i -> i.getCertNo()).collect(Collectors.toSet());

            if (nameSet.size()>1 || certNoSet.size()>1){
                for (MainVO mainVO : entry.getValue()) {
                    System.out.println(JSON.toJSONString(mainVO));
                }
                System.out.println("------------");
            }
        }
    }

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    static class MainVO{
       private String custCode;
       private String custName;
       private String certNo;
    }

}



